package com.itheima.xixi.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.xixi.Dto.DishDto;
import com.itheima.xixi.Dto.SetmealDto;
import com.itheima.xixi.Mapper.SetmealMapper;
import com.itheima.xixi.Service.SetmealService;
import com.itheima.xixi.Service.SetmeralDishService;
import com.itheima.xixi.common.R;
import com.itheima.xixi.entity.Dish;
import com.itheima.xixi.entity.DishFlavor;
import com.itheima.xixi.entity.Setmeal;
import com.itheima.xixi.entity.SetmealDish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmeralDishService setmeralDishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    @Transactional
    //新增套餐，同时保存套餐和菜品的关联关系
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息
        setmeralDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public R<String> removeWithid(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);

        if (count > 0) {
            return R.error("菜品中有起售商品");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(Setmeal::getId, ids);
        setmealService.remove(queryWrapper1);
        return R.success("删除成功！");
    }

    @Transactional
    @Override
    public R<String> statusWithid(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        List<Setmeal> list = setmealService.list(queryWrapper);
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(Setmeal::getId, ids);
        queryWrapper1.eq(Setmeal::getStatus, 0);
        List<Setmeal> list1 = setmealService.list(queryWrapper1);
        if (list != null) {
            for (Setmeal setmeal : list) {
                if (setmeal != null) {
                    setmeal.setStatus(status);
                    setmealService.updateById(setmeal);
                }
            }
        }
        if (list1 != null) {
            for (Setmeal setmeal : list1) {
                if (setmeal != null) {
                    setmeal.setStatus(status);
                    setmealService.updateById(setmeal);
                }
            }
        }
        return R.success("售卖状态修改成功！");

    }
    //查询套餐和套餐分类信息

    @Override
    public SetmealDto getByIdWithid(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询当前套餐的菜品信息，从setmeal_Dish表查询
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmeralDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }
    @Override
    @Transactional
    public void updateWithid(SetmealDto setmealDto) {
        //跟新表基本信息
        this.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmeralDishService.remove(queryWrapper);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes = setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmeralDishService.saveBatch(setmealDishes);

    }
}
